import axios, { AxiosResponse } from 'axios';

const instance = axios.create({
  // baseURL: 'http://81.69.35.249:3096/api',
  baseURL: '/api',
});

instance.interceptors.request.use((value) => {
  return value;
});

instance.interceptors.response.use(
  (response) => {
    const res = response as unknown as AxiosResponse<NormalResponse<any>>;
    if (res.data.code !== 1) {
      alert(res.data.msg);
    }
    console.log(res.status);
    return res.data as any;
  },
  (err) => {
    if (err?.response?.status === 401) {
      window.routerGoto('/login?from=' + location.pathname);
    }
    return Promise.reject(new Error(err?.response?.data?.msg));
  }
);

export default instance;
